package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsCommentBean;
import com.hanson.service.GoodsCommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/admin/comment")
public class GoodsCommentsController {
    @Autowired
    private GoodsCommentService goodsCommentService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "admin/goods/goodsComment/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", goodsCommentService.selectById(Integer.parseInt(id)));
        }
        return "admin/goods/goodsComment/add";
    }

    @RequestMapping(value = "/reply", method = RequestMethod.GET)
    public String reply(ModelMap modelMap, String id) {
        modelMap.put("id", id);
        return "admin/goods/goodsComment/reply";
    }

    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage reply(GoodsCommentBean goodsCommentBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.reply(goodsCommentBean);
        resultMessage.setMsg("回复商品评论成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(GoodsCommentBean goodsCommentBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.addComment(goodsCommentBean, goodsCommentBean.getOpenId());
        resultMessage.setMsg("新增商品评论成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(GoodsCommentBean goodsCommentBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.updateComment(goodsCommentBean);
        resultMessage.setMsg("修改商品评价成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/updateSort/{sort}/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage updateSort(@PathVariable("sort") Integer sort, @PathVariable("id") Integer id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.updateSort(sort, id);
        resultMessage.setMsg("修改评论排序成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/topSort/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage topSort(@PathVariable("id") Integer id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.topComment(id);
        resultMessage.setMsg("置顶评论成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsCommentService.delete(Integer.parseInt(id));
        resultMessage.setMsg("删除商品评论成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<GoodsCommentBean> listData(int limit, int page, GoodsCommentBean goodsCommentBean) {
        Page<GoodsCommentBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(goodsCommentService.listComments(pageInfo, goodsCommentBean));
    }

}
